#!/bin/bash
FILE_ROOT_DIR=$(cd "$(dirname ${BASH_SOURCE[0]})/../.."; pwd)
APPPLUGINS_ROOT_PATH=$(cd "${FILE_ROOT_DIR}/.."; pwd)
FRAMEWORK_DIR=$(cd "${FILE_ROOT_DIR}/../../framework"; pwd)
MODULE_THIRD_DIR=$(cd "${FILE_ROOT_DIR}/../../Module/third_open_src"; pwd)
source ${FRAMEWORK_DIR}/build/common/common.sh
SCRIPT_NAME="${BASH_SOURCE[0]##*/}"
MODULE_LIB_DIR="${FILE_ROOT_DIR}/../../Module/lib"

function copy_file()
{
    # Copy conf files
    mkdir -p  ${PLUGIN_PACKAGE_PATH}/conf
    cp -f ${FILE_ROOT_DIR}/conf/* ${PLUGIN_PACKAGE_PATH}/conf

    # Copy bin script files
    mkdir -p  ${PLUGIN_PACKAGE_PATH}/bin
    cp -f ${FILE_ROOT_DIR}/bin/* ${PLUGIN_PACKAGE_PATH}/bin

    # Copy file plugin library
    NAS_LIB_PATH=${FILE_ROOT_DIR}/build-cmake
    mkdir -p ${PLUGIN_PACKAGE_PATH}/lib/service
    find ${NAS_LIB_PATH} -name "*.so" | xargs -I{} cp -f {} ${PLUGIN_PACKAGE_PATH}/lib/service
    
    # Copy Module library
    find ${MODULE_LIB_DIR} -name "*.so" | xargs -I{} cp -f {} ${PLUGIN_PACKAGE_PATH}/lib/service

    # Copy Scanner library
    SCANNER_DIR=$(cd "${APPPLUGINS_ROOT_PATH}/../FS_Scanner"; pwd)
    SCANNER_LIB_PATH=${SCANNER_DIR}/lib
    find ${SCANNER_LIB_PATH} -name "*.so" | xargs -I{} cp -f {} ${PLUGIN_PACKAGE_PATH}/lib/service

    # Copy Backup library
    BACKUP_DIR=$(cd "${APPPLUGINS_ROOT_PATH}/../FS_Backup"; pwd)
    BACKUP_LIB_PATH=${BACKUP_DIR}/build-cmake
    find ${BACKUP_LIB_PATH} -name "*.so" | xargs -I{} cp -f {} ${PLUGIN_PACKAGE_PATH}/lib/service

    # Copy opensource
    OPENSRC_LIST="lnfs lsmb2 jsoncpp openssl libiconv libssh2 esdk 7z icu curl"
    mkdir -p ${PLUGIN_PACKAGE_PATH}/lib/3rd
    for opensrc in ${OPENSRC_LIST}; do
        find ${MODULE_THIRD_DIR}/${opensrc}_rel -name "*.so*" | xargs -I{} cp -f {} ${PLUGIN_PACKAGE_PATH}/lib/3rd
    done

    find ${MODULE_THIRD_DIR}/../platform/KMCv3_infra_rel -name "*kmcv3.so*" | xargs -I{} cp -f {} ${PLUGIN_PACKAGE_PATH}/lib/3rd
}

function main()
{
    sh ${FRAMEWORK_DIR}/build/generate_full_pkg.sh NasPlugin
}

main "$@"
